export class imageConversion extends plugin {
    constructor() {
        super({
            name: '[SB]图片转换',
            dsc: '缩小/放大 图片',
            event: 'message',
            priority: 50,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^(小|大).*$',
                    /** 执行方法 */
                    fnc: 'ark'
                }
            ]
        })
    }

    async ark(e) {
        let message = e.msg;
        let reduce = message.replace('小', '');
        let amplify = message.replace('大', '');
        reduce = reduce ? parseInt(reduce) : 4;
        amplify = amplify ? parseFloat(amplify) : 2;

        if (e.source) {
            let source
            let count = 0
            // 获取原消息
            if (e.group?.getChatHistory) {
                source = (await e.group.getChatHistory(e.source.seq, 1)).pop()
            } else if (e.friend?.getChatHistory) {
                source = (await e.friend.getChatHistory(e.source.time, 1)).pop()
            }

            for (let data of source.message) {
                if (data.type == 'image') {
                    let height;
                    let width;

                    if (reduce) {
                        height = Math.round( data.height / reduce);
                        width = Math.round( data.width / reduce);

                        data.height = height;
                        data.width = width;
                        logger.info("图片已转换，高：", data.height, " 宽：", data.width);
                        data.file = data.file.replace(/-\d+-\d+/, `-${width}-${height}`);

                        data.summary = '我补药上早八';
                    } else if (amplify) {
                        height = Math.round( data.height * amplify);
                        width = Math.round( data.width * amplify);

                        data.height = height;
                        data.width = width;
                        logger.info("图片已转换，高：", data.height, " 宽：", data.width);
                        data.file = data.file.replace(/-\d+-\d+/, `-${width}-${height}`);

                        data.summary = '我补药上早八';
                    }
                    this.reply(data)
                    count++
                }
            }
            if (!count) {
                logger.info('未找到图片')
            }
            return false
        }
        /** 设置上下文，后续接收到内容会执行doRep方法 */
        this.setContext('doRep')
        logger.warn('请发送图片', false, { at: true })
        
    }

    /** 接受内容 */
    async doRep() {
        let count = 0
        for (let data of this.e.message) {
            if (data.type == 'image') {
                let height;
                let width;

                if (reduce) {
                    height = Math.round( data.height / reduce );
                    width = Math.round( data.width / reduce);

                    data.height = height;
                    data.width = width;
                    logger.info("图片已转换，高：", data.height, " 宽：", data.width);
                    data.file = data.file.replace(/-\d+-\d+/, `-${width}-${height}`);

                    data.summary = '我补药上早八';
                } else if (amplify) {
                    height = Math.round( data.height * amplify );
                    width = Math.round( data.width * amplify);

                    data.height = height;
                    data.width = width;
                    logger.info("图片已转换，高：", data.height, " 宽：", data.width);
                    data.file = data.file.replace(/-\d+-\d+/, `-${width}-${height}`);

                    data.summary = '我补药上早八';
                }
                this.reply(data)
                count++;
            }
        }
        if (!count) {
            logger.info('未找到图片')
        }
        /** 结束上下文 */
        this.finish('doRep')
    }
}